<?php

class signatureTool {
	var $signatureImage;
	var $lastTextSize;
	function createFromBackground($image) {
		$this->signatureImage = ImageCreateFromPNG($image);
	}
	function colorAllocate($color,$format='hex') {
		if($format == 'hex') {
	  $colorArray = sscanf($color, '#%2x%2x%2x');
	 }
	 else {
	  $colorArray = $color;
	 }
	 return ImageColorAllocate($this->signatureImage, $colorArray[0], $colorArray[1], $colorArray[2]);
	}
	function insertText($x,$y,$size,$angle,$color,$fontfile,$text,$outline=false,$xAlign='left') {
		$this->lastTextSize = imagettfbbox($size,$angle,$fontfile,$text);
		if($xAlign == 'right') {
	  $boxSize = $this->lastTextSize;
	  $x = 500 - $boxSize[2] - $x;
	 }
	 if($outline) {
	  $colorArray = sscanf($color, '#%2x%2x%2x');
	  // adjust outline color
	  $luma = $colorArray[0]*0.21+$colorArray[1]*0.71+$colorArray[2]*0.072;
	  for($i=0;$i<4;$i++) {
	  	$outlineColor[$i] = abs($luma-255);
	  	if($outlineColor[$i] > 80 && $outlineColor[$i] < 127.5) {
	  		$outlineColor[$i] = 80;
	  	}
	  	if($outlineColor[$i] >= 127.5 && $outlineColor[$i] < 185) {
	  		$outlineColor[$i] = 185;
	  	}
	  }
	  $colorOutline = $this->colorAllocate($outlineColor,'array');
	  for($i=0;$i<9;$i++) { // calculate outline position
	  	$xOutline = $x+round(sin($i*(pi()/4)));
	  	$yOutline = $y+round(cos($i*(pi()/4)));
	  	imagettftext($this->signatureImage,$size,$angle,$xOutline,$yOutline+$size,$colorOutline,$fontfile,$text);
	  }
	 }
	 imagettftext($this->signatureImage,$size,$angle,$x,$y+$size,$this->colorAllocate($color),$fontfile,$text);

	}
	function createImage() {
		header("Expires: ".gmdate("D, d M Y H:i:s", mktime() + 18000)." GMT");
		header("Cache-Control: max-age=18000, must-revalidate");
		header('Content-type: image/png');
		imagepng($this->signatureImage);
		imagedestroy($this->signatureImage);
	}
	function insertTextBox($x,$y,$x1,$y1,$size,$borderColor,$textColor,$backgroundColor,$fontfile,$text) {
		imagefilledrectangle($this->signatureImage,$x,$y,$x1,$y1,$this->colorAllocate($backgroundColor));
		imagerectangle($this->signatureImage,$x-1,$y-1,$x1+1,$y1+1,$this->colorAllocate($borderColor));
		$textSize = imagettfbbox($size,0,$fontfile,$text);
		# center text on x axis
		$textWidth = $textSize[2] - $textSize[0];
		$textLeftSpace = (18 - $textWidth)/2;
		$textX = $x + $textLeftSpace;
		# center text on y axis
		$textHeight = $textSize[1] - $textSize[7];
		$textTopSpace = (18 - $textHeight)/2;
		$textY = $y + $textTopSpace;
		imagettftext($this->signatureImage,$size,0,$textX+1,$textY+$size,$this->colorAllocate($textColor),$fontfile,$text);
	}
	function insertImage($x,$y,$url,$type,$border=false,$color='#FFFFFF') {
		$imageSize = getimagesize($url);
		switch($type) {
			case 'gif':
				$imageImport = ImageCreateFromGIF($url);
				break;
			case 'jpeg':
				$imageImport = ImageCreateFromJPEG($url);
				break;
	  case 'png':
	   $imageImport = ImageCreateFromPNG($url);
	   break;
	 }
	 ImageCopy($this->signatureImage,$imageImport,$x,$y,0,0,$imageSize[0],$imageSize[1]);
	 if($border) {
	 	imagerectangle($this->signatureImage,$x-1,$y-1,$x+$imageSize[0],$y+$imageSize[1],$this->colorAllocate($color));
	 }
	}
	function drawLine($x1,$y1,$x2,$y2,$color) {
		imageline($this->signatureImage,$x1,$y1,$x2,$y2,$this->colorAllocate($color));
	}
}
?>